package com.jinyijia.api.infrastructure;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jinyijia.api.rest.v1.order.dto.OrderDTO;
import com.jinyijia.api.rest.v1.order.vo.GoodsVO;
import com.jinyijia.api.rest.v1.order.vo.OrderDetailVO;
import com.jinyijia.api.rest.v1.order.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author lhw
 * @date 2018/8/11
 **/
@Service
public class OrderManager {

    @Autowired
    @LoadBalanced
    private RestTemplate restTemplate;

    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public JSONArray tabs(Integer customId) {
        JSONArray tabs = new JSONArray();
        JSONObject tab = new JSONObject();
        tab.put("name", "全部");
        tab.put("order_form", allOrders(customId));
        tabs.add(tab);
        tab = new JSONObject();
        tab.put("name", "已付款");
        tab.put("order_form", statusOrders(customId, "PART_REVIEW"));
        tabs.add(tab);
        tab = new JSONObject();
        tab.put("name", "待付款");
        tab.put("order_form", statusOrders(customId, "SUBMIT"));
        tabs.add(tab);
        tab = new JSONObject();
        tab.put("name", "已确认");
        tab.put("order_form", statusOrders(customId, "CONFIRM"));
        tabs.add(tab);
        return tabs;
    }

    private List<OrderVO> allOrders(Integer customId) {
        JSONObject orders = restTemplate.getForObject("http://order-service/orders?customInfo.id={1}&size=10000", JSONObject.class, customId);
        List<OrderDTO> orderDTOS = orders.getJSONArray("content").toJavaList(OrderDTO.class);
        List<OrderVO> orderVOS = orderDTOS.stream().map(orderDTO -> new OrderVO(orderDTO.getShopInfo().getName(), dateFormat.format(orderDTO.getCreateTime()), orderDTO.getCode(), orderDTO.getPaymentInfo().size() > 0 ? (orderDTO.getPaymentInfo().get(0).getPayType() == 0 ? "线下支付" : "其他") : "——", orderDTO.getItems().stream().map(orderItemDTO -> new GoodsVO(Objects.nonNull(orderItemDTO.getGoodsInfo().getImageUrl()) ? orderItemDTO.getGoodsInfo().getImageUrl().split(",")[0] : "", orderItemDTO.getGoodsInfo().getName(), orderItemDTO.getGoodsInfo().getStandard(), orderItemDTO.getGoodsInfo().getSkuCode(), orderItemDTO.getAmount().doubleValue() / 100, orderItemDTO.getCount())).collect(Collectors.toList()), orderDTO.getShipAddress().getName(), orderDTO.getAmount().doubleValue() / 100, orderDTO.getFreight().doubleValue() / 100, orderDTO.getStatus().getName())).collect(Collectors.toList());
        return orderVOS;
    }

    private List<OrderVO> statusOrders(Integer customId, String status) {
        JSONObject orders = restTemplate.getForObject("http://order-service/orders?customInfo.id={1}&status={2}&size=10000", JSONObject.class, customId, status);
        List<OrderDTO> orderDTOS = orders.getJSONArray("content").toJavaList(OrderDTO.class);
        List<OrderVO> orderVOS = orderDTOS.stream().map(orderDTO -> new OrderVO(orderDTO.getShopInfo().getName(), dateFormat.format(orderDTO.getCreateTime()), orderDTO.getCode(), orderDTO.getPaymentInfo().size() > 0 ? (orderDTO.getPaymentInfo().get(0).getPayType() == 0 ? "线下支付" : "其他") : "——", orderDTO.getItems().stream().map(orderItemDTO -> new GoodsVO(Objects.nonNull(orderItemDTO.getGoodsInfo().getImageUrl()) ? orderItemDTO.getGoodsInfo().getImageUrl().split(",")[0] : "", orderItemDTO.getGoodsInfo().getName(), orderItemDTO.getGoodsInfo().getStandard(), orderItemDTO.getGoodsInfo().getSkuCode(), orderItemDTO.getAmount().doubleValue() / 100, orderItemDTO.getCount())).collect(Collectors.toList()), orderDTO.getShipAddress().getName(), orderDTO.getAmount().doubleValue() / 100, orderDTO.getFreight().doubleValue() / 100, orderDTO.getStatus().getName())).collect(Collectors.toList());
        return orderVOS;
    }

    public OrderDetailVO orderDetail(String code, Integer customId) {
        JSONObject orders = restTemplate.getForObject("http://order-service/orders?code={1}&customInfo.id={2}", JSONObject.class, code, customId);
        OrderDTO orderDTO = orders.getJSONArray("content").toJavaList(OrderDTO.class).get(0);
        OrderDetailVO orderDetailVO = new OrderDetailVO(0, "OK", orderDTO.getStatus().getName(), orderDTO.getCode(), dateFormat.format(orderDTO.getCreateTime()), orderDTO.getPaymentInfo().size() > 0 ? dateFormat.format(orderDTO.getPaymentInfo().get(0).getPayTime()) : "——", Objects.nonNull(orderDTO.getAuditTime()) ? dateFormat.format(orderDTO.getAuditTime()) : "——", orderDTO.getPaymentInfo().size() > 0 ? (orderDTO.getPaymentInfo().get(0).getPayType() == 0 ? "线下支付" : "其他") : "——", orderDTO.getShipAddress().getName(), orderDTO.getShipAddress().getPhone(), orderDTO.getShipAddress().getProvince() + " " + orderDTO.getShipAddress().getCity() + " " + orderDTO.getShipAddress().getDistrict() + " " + orderDTO.getShipAddress().getTown() + " " + orderDTO.getShipAddress().getRoad(), orderDTO.getItems().stream().map(orderItemDTO -> new GoodsVO(Objects.nonNull(orderItemDTO.getGoodsInfo().getImageUrl()) ? orderItemDTO.getGoodsInfo().getImageUrl().split(",")[0] : "", orderItemDTO.getGoodsInfo().getName(), orderItemDTO.getGoodsInfo().getStandard(), orderItemDTO.getGoodsInfo().getId(), orderItemDTO.getAmount().doubleValue() / 100, orderItemDTO.getCount())).collect(Collectors.toList()), orderDTO.getAmount().doubleValue() / 100, orderDTO.getFreight().doubleValue() / 100, orderDTO.getPaymentInfo().size() > 0 ? orderDTO.getPaymentInfo().get(0).getAmount().doubleValue() / 100 : 0,orderDTO.getShopInfo().getName());
        return orderDetailVO;
    }
}
